package com.pzhu.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pzhu.mapper.ReviewMapper;
import com.pzhu.mapper.ThaliImageMapper;
import com.pzhu.model.entity.Review;
import com.pzhu.model.entity.ThaliImage;
import com.pzhu.model.help.ResponseResult;
import com.pzhu.model.vo.MyReviewVo;
import com.pzhu.service.ReviewService;
import com.pzhu.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author : zqh
 * @Date : 2023/12/14 0014 下午 11:09
 * @Version: 1.0
 */
@Service
public class ReviewServiceImpl extends ServiceImpl<ReviewMapper, Review> implements ReviewService {

    @Autowired
    private ReviewMapper reviewMapper;

    @Autowired
    private ThaliImageMapper thaliImageMapper;

    @Override
    public ResponseResult findAll(Long userId) {

        List<Review> reviewList = reviewMapper
                .selectList(new LambdaQueryWrapper<Review>()
                        .eq(Review::getUserId, userId));

        List<MyReviewVo> reviewVoList = BeanCopyUtils.copyBeanList(reviewList, MyReviewVo.class);
        reviewVoList.forEach(v->{
            List<ThaliImage> categoryImages = thaliImageMapper.selectList(
                    new LambdaQueryWrapper<ThaliImage>()
                            .eq(ThaliImage::getThaliId, v.getThaliId()));
            v.setPhoto(categoryImages.get(0).getUrl());
        });

        Map<String,Object> map = new HashMap<>();
        map.put("list",reviewVoList);

        return ResponseResult.ok(map);
    }
}
